<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class Page extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\PageModel();
        $this->RouteModel=new \app\admin\model\RouteModel();
        $this->validate=new \app\admin\validate\PageValidate();
    }  
    //单页信息
    public function index(){  
        $data=$this->model->order('page_id desc')->select();
        View::assign("data",$data);
        return View::fetch();
    }
    public function add(){  
        if(request()->isPost()){
            $data=$this->request->param();
            //如果为页面式去掉选择模板的验证条件
            if($data['pagemod']==1){
                unset($this->validate->rule['template_id']);
            }
            //
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }  
            $this->model->save($data);            
            $lastId=$this->model->page_id;
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            if($data['pagemod']==1){
                $this->model->makePageTemplateFile($lastId);
            }     
            $this->RouteModel->updatePageRoute();//更新路由
            $this->success($data);
        }else{
            View::assign("pagetemp",Db::name("template")->where("type","page")->order("sort asc,template_id desc")->select());
            View::assign("pubid",make_pubid());
            return View::fetch();
        }        
    }  
    public function update(){
        if(request()->isPost()){
            $data=$this->request->param();
            //如果为页面式去掉选择模板的验证条件
            if($data['pagemod']==1){
                unset($this->validate->rule['template_id']);
            }
            //
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            } 
            //记录历吏版本
            $this->setHistory($data['page_id']);
            $this->model->update($data);
            if($data['pagemod']==1){
                $this->model->makePageTemplateFile($data['page_id']);
            }else{
                //如果不是页面式，删除模板文件
                $this->model->deletePageTemplateFile($data['page_id']);
            }
            $this->RouteModel->updatePageRoute();//更新路由
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            $this->success($data);
         
        }else{
            $page_id=Request::param("page_id",0,"intval");        
            $r=$this->model->find($page_id);
            $pagemod=Request::param("pagemod")?Request::param("pagemod"):$r['pagemod'];
            View::assign("pagemod",$pagemod);
            View::assign("pagetemp",Db::name("template")->where("type","page")->order("sort asc,template_id desc")->select());
            View::assign("r",$r);
            return View::fetch();
        }        
    }  
    //删除页面
    public function delete(){
        $page_id=Request::param('page_id');
        $template=$this->model->find($page_id)->toArray();
        //删除模板表记录
        $this->model->where("page_id",$page_id)->delete();
        //移到回收站表
        $template["delete_time"]=time();
        Db::name("page_recycle")->replace()->insert($template);     
        //删除文件
        $this->model->deletePageTemplateFile($page_id);   
        //
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
        $this->RouteModel->updatePageRoute();//更新路由
        $this->success("删除成功");  
    }    
    //单页模板
    public function page_template(){
        $where["type"]="page";		
        $data=Db::name("template")->where($where)->order('sort asc,template_id desc')->select();
        View::assign("data",$data);
        return View::fetch();        
    }   
    public function page_template_add(){        
        return View::fetch();  
    }      
    public function page_template_update(int $template_id=0){        
        $r=Db::name("template")->find($template_id);
        View::assign("r",$r);
        return View::fetch();  
    }   
    //记录历吏修改
    private function setHistory($page_id){
        //老的模板内容（未修改前的）
        $old_template=Db::name("page")->where(array("page_id"=>$page_id))->value("pagetext");
        //获取post过来的新的模板内容
        $new_template=Request::param("pagetext");
        //如果没有老的，或者没有新的则不做版本标记
        if(!$old_template||!$new_template){
            return;
        }
        //如果老的模板内容和新的模板内容一样也不做版本标记
        if($old_template==$new_template){
            return;
        }
        //
        $data["page_id"]=$page_id;
        $data["addtime"]=time();
        $data["userid"]=$this->user_id;
        $data["username"]=$this->username;
        $data["content"]=$old_template;
        Db::name("page_history")->save($data);
        //查询备份的模板数据是否大于设置的值，多余的删除
        $maxBake=config("app.max_template_bake");
        $count=Db::name("page_history")->where(array("page_id"=>$page_id))->count();
        if($count>$maxBake){
            $min_id=Db::name("page_history")->where(array("page_id"=>$page_id))->min("id");
            Db::name("page_history")->delete($min_id);
        }
    }    
}